
#include <asm.h>

.global C_SYMBOL(WaveRender)
.global C_SYMBOL(WaveSpread)


C_SYMBOL(WaveSpread):

    STMFD   SP!, {R4-R11,LR}
    LDR     R4, [R0]
    LDR     R12, [R1]

    MUL     LR, R2,R3

    MOV     R3, R0
    MOV     R7, R1

    MOV     R10, R2, LSL #1

    LDR     R0, =0x10001
    TMCRR   wR12 , R0,R0
    LDR     R6, =0x50005
    TMCRR   wR13 , R6,R6

    MOV     R1, R4
    MOV     R0, R12

    WLDRD   wR5, [R1, #-8]
    SUB     R5,  R1, R10
    WLDRD   wR8, [R5]
    ADD     R5,  R1, R10
    WLDRD   wR9, [R5]
    WLDRD   wR6, [R1], #8

    WLDRD   wR7, [R1], #8

    WLDRD   wR10, [R0]

Loop:

    PLD	[R0,#0x30]

    WALIGNI wR0, wR5, wR6, #6
    WMOV   wR5, wR6
    WALIGNI wR1, wR6, wR7, #2
    WMOV   wR6, wR7

    WADDH   wR0, wR0, wR8
    WADDH   wR0, wR0, wR9

    SUB     R5,  R1, R10
    PLD	    [R5,#0x30]
    WLDRD   wR8, [R5, #-8]

    WADDH   wR0, wR0, wR1

    ADD     R5,  R1, R10
    PLD	    [R5,#0x30]
    WLDRD   wR9, [R5, #-8]

    WSRAH   wR0,wR0,wR12

    WLDRD   wR7, [R1], #8

    WSUBH   wR0,wR0,wR10

    WLDRD   wR10, [R0, #8]

    WSRAH   wR4,wR0,wR13
    WSUBH   wR0,wR0,wR4

    WSTRD   wR0, [R0], #8



    WALIGNI wR0, wR5, wR6, #6
    WMOV   wR5, wR6
    WALIGNI wR1, wR6, wR7, #2
    WMOV   wR6, wR7

    WADDH   wR0, wR0, wR8
    WADDH   wR0, wR0, wR9

    SUB     R5,  R1, R10
    WLDRD   wR8, [R5, #-8]

    WADDH   wR0, wR0, wR1

    ADD     R5,  R1, R10
    WLDRD   wR9, [R5, #-8]

    WSRAH   wR0,wR0,wR12

    WLDRD   wR7, [R1], #8

    WSUBH   wR0,wR0,wR10

    WLDRD   wR10, [R0, #8]

    WSRAH   wR4,wR0,wR13
    WSUBH   wR0,wR0,wR4

    WSTRD   wR0, [R0], #8

    SUBS    LR, LR, #8
    BGE     Loop

    STR     R12, [R3]
    STR     R4, [R7]
    LDMFD   SP!, {R4-R11,PC}


C_SYMBOL(WaveRender):
		STMFD	SP!, {R4-R8,LR}
		MOV	R7, R2
		MOV	R5, R3
		MOV	R12, #0xF0
		MOV	R6, R12
		LDR	R4, =0x12B0F

MainLoop:
		MOV	LR, R12,LSL#1
		ADD	R3, LR,	R0
		LDRSH	R1, [R3,#-2]
		LDRSH	R2, [R3,#2]
		RSB	R1, R2,	R1
		SUB	R2, R3,	#0x1E0
		LDRSH	R2, [R2]
		ADD	R3, R3,	#0x1E0
		LDRSH	R3, [R3]
		RSB	R2, R3,	R2
		MLA	R3, R2,	R6, R1
		ADD	R2, R12, R3
		SUB	R3, R2,	#1
		CMP	R3, R4
		MOVLS	R3, R2,LSL#1
		LDRLSH	R3, [R3,R7]
		STRLSH	R3, [LR,R5]
		ADD	R12, R12, #1
		CMP	R12, R4
		BLE	MainLoop

		LDMFD	SP!, {R4-R8,PC}


_WaveRender2:
		STMFD	SP!, {R4-R8,LR}
		MOV	R7, R2
		MOV	R5, R3
		MOV	R12, #0xF0
		MOV	R6, R12
		LDR	R4, =0x12B0F


loc_1EC:
		MOV	LR, R12,LSL#1
		ADD	R3, LR,	R0
		LDRSH	R1, [R3,#-2]
		LDRSH	R2, [R3,#2]
		RSB	R1, R2,	R1
		SUB	R2, R3,	#0x1E0
		LDRSH	R2, [R2]
		ADD	R3, R3,	#0x1E0
		LDRSH	R3, [R3]
		RSB	R2, R3,	R2
		MLA	R3, R2,	R6, R1
		ADD	R2, R12, R3
		SUB	R3, R2,	#1
		CMP	R3, R4
		MOVLS	R3, R2,LSL#1
		LDRLSH	R3, [R3,R7]
		STRLSH	R3, [LR,R5]
		ADD	R12, R12, #1
		CMP	R12, R4
		BLE	loc_1EC

		LDMFD	SP!, {R4-R8,PC}
